

package net.jjjshop.front.scheduled;

import lombok.extern.slf4j.Slf4j;
import net.jjjshop.common.cache.RedisUtil;
import net.jjjshop.common.constants.TaskConstants;
import net.jjjshop.common.service.lbt.BuyProductSettlementService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 订单定时任务、未付款关闭，自动确认收货，订单结算
 */

@Slf4j
@Component
@EnableScheduling // 2.开启定时任务
public class BuyScheduled {
    @Autowired
    private BuyProductSettlementService buyProductSettlementService;
    @Autowired
    private RedisUtil redisUtil;

    private static final Logger logger = LoggerFactory.getLogger(BuyScheduled.class);

    /**
     * 每分钟执行一次
     */
    @Scheduled(fixedRate = 10000)  //每10秒执行一次
    public void init() throws Exception {

        String redisKey = TaskConstants.ORDER_TASK_REDIS_KEY_AFTER_TAKE_BY_USER;
        Long size = redisUtil.getListSize(redisKey);
        if (size < 1) {
            return;
        }
        Object id = redisUtil.getRightPop(redisKey, 10L);
        try {
            Boolean result = buyProductSettlementService.orderReceiving(Integer.valueOf(id.toString()));
            if (!result) {
                redisUtil.lPush(redisKey, id);
                logger.info("订单支付订单任务异常，result异常，订单号id为: " + id);
            }
            if (result){
                logger.info("订单支付订单任务完成，result返回true，订单号id为: " + id);
            }
        } catch (Exception e) {
            logger.info(e.getMessage());
            logger.info("订单支付订单任务异常，catch打印，订单号id为: " + id);
        }
    }


}
